//
//  LLBTabBarExtension.swift
//  LLBBaseVC
//
//  Created by Yun on 2018/11/1.
//

import UIKit

extension UITabBar{
    override open func layoutSubviews() {
        super.layoutSubviews()
        if let UITabBarButtonClass = NSClassFromString("UITabBarButton") as? NSObject.Type{
            let subItems = self.subviews.filter({return $0.isKind(of: UITabBarButtonClass)})
            if subItems.count > 0{
                let tmpWidth = UIScreen.main.bounds.width / CGFloat(subItems.count)
                for (index,item) in subItems.enumerated(){
                    item.frame = CGRect(x: CGFloat(index) * tmpWidth, y: 0, width: tmpWidth, height: item.bounds.height)
                }
            }
        }
    }
    
    override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let view:UITabBar = super.hitTest(point, with: event) as? UITabBar{
            for item in view.subviews{
                if point.x >= item.frame.origin.x  && point.x <= item.frame.origin.x + item.frame.size.width{
                    return item
                }
            }
        }
        return super.hitTest(point, with: event)
    }
}
